/*
 * @Author: ygnnli 1585923072@qq.com
 * @Date: 2024-08-15 14:28:21
 * @LastEditors: ygnnli 1585923072@qq.com
 * @LastEditTime: 2024-08-17 17:22:22
 * @FilePath: /bitrpc/source/test_2.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEme
 */
#pragma once
#include "message.hpp"

int main()
{
    //bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::REQ_RPC);
    //dynamic_pointer_cast:如果bmp是RpcRequest类型，就转换成RpcRequest类型，否则返回nullptr
    //bitrpc::RpcRequest::ptr rrp = std::dynamic_pointer_cast<bitrpc::RpcRequest>(bmp);

    //创建RpcRequest类型的消息
    // bitrpc::RpcRequest::ptr rrp = bitrpc::MessageFactory::create<bitrpc::RpcRequest>();
    // Json::Value param;
    // param["num1"] = 11;
    // param["num2"] = 22;
    // rrp->setMethod("Add");
    // rrp->setParams(param);
    // std::string str = rrp->serialize();
    // std::cout<<str<<std::endl;

    //创建TopicRequest类型的消息
    // bitrpc::TopicRequest::ptr trp = bitrpc::MessageFactory::create<bitrpc::TopicRequest>();
    // trp->setTopicKey("news");
    // trp->setOptype(bitrpc::TopicOpType::TOPIC_PUBLISH);//发布主题
    // trp->setTopicMsg("hello world");
    // std::string str = trp->serialize();
    // std::cout<<str<<std::endl;

    // bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::REQ_TOPIC);//根据消息类型创建消息
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // bitrpc::TopicRequest::ptr trp2 = std::dynamic_pointer_cast<bitrpc::TopicRequest>(bmp);
    // std::cout<<trp2->topicKey()<<std::endl;
    // std::cout<<(int)trp2->optype()<<std::endl;
    // std::cout<<trp2->topicMsg()<<std::endl;

    //创建ServiceRequest类型的消息
    // bitrpc::ServiceRequest::ptr trp = bitrpc::MessageFactory::create<bitrpc::ServiceRequest>();
    // trp->setMethod("Add");
    // trp->setOptype(bitrpc::ServiceOptype::SERVICE_REGISTRY);//注册服务
    // trp->setHost(bitrpc::Address("127.0.0.1",9090));//注册服务时，需要设置服务的主机和端口
    // std::string str = trp->serialize();
    // std::cout<<str<<std::endl;

    // bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::REQ_SERVICE);//根据消息类型创建消息
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // bitrpc::ServiceRequest::ptr trp2 = std::dynamic_pointer_cast<bitrpc::ServiceRequest>(bmp);
    // std::cout<<trp2->method()<<std::endl;
    // std::cout<<(int)trp2->optype()<<std::endl;
    // std::cout<<trp2->host().first<<std::endl;
    // std::cout<<trp2->host().second<<std::endl;

    //创建RpcResponse类型的消息
    // bitrpc::RpcResponse::ptr trp = bitrpc::MessageFactory::create<bitrpc::RpcResponse>();
    // trp->setRCode(bitrpc::RCode::RCODE_OK);
    // trp->setResult(33);
    // std::string str = trp->serialize();
    // std::cout<<str<<std::endl;

    // bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::RSP_RPC);//根据消息类型创建消息
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // bitrpc::RpcResponse::ptr trp2 = std::dynamic_pointer_cast<bitrpc::RpcResponse>(bmp);
    // std::cout<<(int)trp2->rcode()<<std::endl;
    // std::cout<<trp2->result().asInt()<<std::endl;

    //创建TopicResponse类型的消息
    // bitrpc::TopicResponse::ptr trp = bitrpc::MessageFactory::create<bitrpc::TopicResponse>();
    // trp->setRCode(bitrpc::RCode::RCODE_OK);
    // std::string str = trp->serialize();
    // std::cout<<str<<std::endl;

    // bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::RSP_TOPIC);//根据消息类型创建消息
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // bitrpc::TopicResponse::ptr trp2 = std::dynamic_pointer_cast<bitrpc::TopicResponse>(bmp);
    // std::cout<<(int)trp2->rcode()<<std::endl;


    bitrpc::ServiceResponse::ptr trp = bitrpc::MessageFactory::create<bitrpc::ServiceResponse>();
    trp->setRCode(bitrpc::RCode::RCODE_OK);
    trp->setMethod("Add");
    trp->setOptype(bitrpc::ServiceOptype::SERVICE_DISCOVERY);//设置服务发现
    std::vector<bitrpc::Address> addrs;
    addrs.push_back(bitrpc::Address("127.0.0.1", 9090));
    addrs.push_back(bitrpc::Address("127.0.0.2", 9091));
    trp->setHost(addrs);
    std::string str = trp->serialize();
    std::cout<<str<<std::endl;

    bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::RSP_SERVICE);//根据消息类型创建消息
    bool ret = bmp->unserialize(str);
    if(ret == false)
    {
        return -1;
    }
    ret = bmp->check();
    if(ret == false)
    {
        return -1;
    }
    bitrpc::ServiceResponse::ptr trp2 = std::dynamic_pointer_cast<bitrpc::ServiceResponse>(bmp);
    std::cout<<(int)trp2->rcode()<<std::endl;
    std::cout<<trp2->method()<<std::endl;
    std::cout<<(int)trp2->optype()<<std::endl;
    std::vector<bitrpc::Address> addrs1 = trp2->hosts();
    for(auto addr:addrs1)
        std::cout<<addr.first<<":"<<addr.second<<std::endl;

    


    // bitrpc::BaseMessage::ptr bmp = bitrpc::MessageFactory::create(bitrpc::MType::REQ_RPC);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // bitrpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<bitrpc::RpcRequest>(bmp);
    // std::cout<<rrp2->method()<<std::endl;
    // std::cout<<rrp2->params()["num1"].asInt()<<std::endl;
    // std::cout<<rrp2->params()["num2"].asInt()<<std::endl;
    return 0;
}